<?php

class Add_xls extends Controller
{

	function index()
	{
	
	}
//to upload xls file and read content of it....
	function add_xls()
	{
		
		parent::Controller();	
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library(array('form_validation','session'));
		ini_set("display_errors",1);
		error_reporting(E_ALL);
	
    $user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {    //echo FCPATH;
    //exit;
    		$displayform ='';
        
        $displayform .='<div id=dashboard ><span>Upload Excel Sheet</span>&nbsp;<a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a>&nbsp;<img align=right src="'.base_url().'images/back.png" id="back"" alt="back" ONCLICK="history.go(-1)" /></div>';
        $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div>';
        $displayform .='<br/>';
    		$attr = array('class'=>'add_xls','id'=>'add_xls');
    		$displayform .= form_open_multipart('index.php/add_xls/save_xls',$attr);
    	    	    	
    	    	$displayform .= '<fieldset id="legendstyle">';
    	    	$displayform .= ' <legend id="legendtext">Please select file to upload</legend>';
    	    	$displayform .= '<center><table cellspacing="20px"><tr>';
    	   		
    		$displayform .= '<tr><td align=left>select a file to upload <em> * </em></td>';
    		$displayform .= '<td align=left><input type="file" name="userfile" id="userfile" size="20" class="required" accept="xls" />';
    		$displayform .= '</td></tr>';
    
    		$displayform .='<tr><td colspan=2 align=center><input class="submit" type="submit" value="Upload Excel Sheet"/>';
    		$displayform .='</td></tr></table></center>';
        $displayform .= '</fieldset><div align="right"></form></div>';
    		
    		
    		
    	//	$this->template->add_js('js/jquery-latest.js');	
    	//	$this->template->add_js('js/jquery.validate.js');
    
    		$this->template->add_js('$(document).ready(function(){
    	 		   $("#add_xls").validate();
    	  			});
    		','embed');
        
        $this->template->add_js('
              $(document).ready(function(){
                  $("#userfile").change(function(){
                          var file = $("#userfile").val();
                            $.post("/data_export/check_file", {
                              file_name:file },
                              
                          function(data){
                            alert(data);
                                    }, "json");
                          
                    
                  })  
              });
              
        ','embed');
        
    		  $data = array('formdisplay'=>$displayform);
    
    			$this->template->write_view('header','header', $data, True);
    			$this->template->write_view('content', 'other_general');
    			$this->template->render();
          
    		}
      else
        {
          echo "Forbidden to access!!";
        }
		}
		
	

		
		function save_xls()
		{
		
		 $user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {	
    		//	$dir = './cafuploads/';
      //  echo $config['file_path'];exit;
        
    		$config['upload_path'] = './cafuploads/';
        //echo $config['upload_path'];
    		$config['allowed_types'] = 'xls';
    		$config['max_size']	= '2097152';
    		$config['max_width']  = '1024';
    		$config['max_height']  = '768';
    
    
    		$this->load->library('upload',$config);
    				
    		if (!$this->upload->do_upload()) 
    				{
    					$error = array('error' => $this->upload->display_errors());
    					print_r($error);
    				}    
    				else
    				{	
    					$filename = array('upload_data' => $this->upload->data());
    				
    					
              
                      
         
        //  $data = new Spreadsheet_Excel_Reader('./cafuploads/'.$filename['upload_data']['file_name']);
         // echo $data->dump(true,true);
    
    					$this->load->model('readbatchcard_model');
              
    					$this->readbatchcard_model->read_xlsdata($filename['upload_data']['file_name']);
            
              
            //  redirect("/idea_home/admin_dashboard");
    										
    				}
    }
    else
    {
      echo "Forbidden to access";
    }
  				
		}
		
	 
}

